package com.woniuxy.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.TbBedDao;
import com.woniuxy.daos.TbFixDao;
import com.woniuxy.daos.TbMalfunctionDao;
import com.woniuxy.entitys.TbFix;
import com.woniuxy.services.FixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class FixServiceImpl implements FixService {
    @Autowired
    private TbFixDao tbFixDao;

    @Override
    public PageInfo<TbFix> getFixList(Integer pageNum, Integer pageSize, Integer hid, Integer did, Integer uid, Integer softdel) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbFix> list =  tbFixDao.selectFixList(hid,did,uid,softdel);
        PageInfo<TbFix> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<TbFix> getFixListWithTime(Integer pageNum, Integer pageSize, Integer hid, Integer did, Integer uid, Integer softdel, Date beginTime, Date endTime) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbFix> list =  tbFixDao.selectFixListWithTime(hid,did,uid,softdel,beginTime,endTime);
        PageInfo<TbFix> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public TbFix getFixDetailByFid(Integer fid) {
        return tbFixDao.selFixDetailByFid(fid);
    }


    /**
     * 若前端传来的softdel为0（已修复）,则增加一条维修记录后同时修改床的状态为空闲0，故障记录状态为已修复2
     * 若前端传来的softdel为1（无法修复）,则增加一条维修记录后同时修改故障状态为无法修复2
     */
    @Autowired
    private TbBedDao tbBedDao;
    @Autowired
    private TbMalfunctionDao tbMalfunctionDao;
    @Transactional
    @Override
    public Integer saveFix(TbFix tbFix) {
        if(tbFix.getSoftdel()==0){//0已修复
            tbBedDao.updateStatusToZero(tbFix.getBid());
            tbMalfunctionDao.updateStatus(2,tbFix.getMid());
        }else{
            tbBedDao.updateStatusToTwo(tbFix.getBid());
            tbMalfunctionDao.updateStatus(3,tbFix.getMid());
        }
        return tbFixDao.insert(tbFix);
    }

    @Override
    public TbFix getFixByFid(Integer fid) {
        return tbFixDao.selByFid(fid);
    }

    @Override
    @Transactional
    public Integer saveUpdFix(TbFix tbFix) {
        if(tbFix.getSoftdel()==0){//0已修复
            tbBedDao.updateStatusToZero(tbFix.getBid());
            tbMalfunctionDao.updateStatus(2,tbFix.getMid());
        }else{
            tbBedDao.updateStatusToTwo(tbFix.getBid());
            tbMalfunctionDao.updateStatus(3,tbFix.getMid());
        }
        return tbFixDao.updateByPrimaryKeySelective(tbFix);
    }
}
